package cinema.ELTEcinema;

import java.io.Serializable;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.enterprise.context.SessionScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;

import cinema.ELTEcinema_ejb.Dao.RoomDAO;
import cinema.ELTEcinema_ejb.Entity.Room;

@SuppressWarnings("serial")
@Named
@SessionScoped
public class RoomController implements Serializable{

	@Inject
	private RoomDAO roomDAO;

	private String name;
	private Room room;

	private List<Room> rooms;

	@PostConstruct
	public void init(){
		rooms = roomDAO.findAll();
	}

	public List<Room> getRooms() {
		return rooms;
	}

	public void addRoom() {		 
		Room room = new Room(name);
		rooms.add(room);
		roomDAO.save(room);
	}

	public void deleteRoom() {
		try{
			roomDAO.delete(room);
			rooms.remove(room);	
		}
		catch(Exception e){
			FacesContext.getCurrentInstance().addMessage("movieTable",
                new FacesMessage(FacesMessage.SEVERITY_ERROR, "cannot delete room if showing is registered for it", "cannot delete room if showing is registered for it"));
		}
	}

	public void editRoom(){
		room.setCanEdit(true);
	}

	public void saveRoom(){
		room.setCanEdit(false);
		roomDAO.update(room);
	}

	public String getName() {
		return name;
	}

	public void setname(String name) {
		this.name = name;
	}

	public Room getRoom() {
		return room;
	}

	public void setRoom(Room room) {
		this.room = room;
	}
}